/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.dao;

import java.util.List;

import net.iwsh.core.db.ext.DialectSyntaxParser;
import net.iwsh.core.db.ext.SQLWithParams;

import org.apache.commons.lang3.StringUtils;

import com.us.usws.model.domain.video.VideoFile;
import com.us.usws.model.flat.video.DOVideoFile;
import com.us.usws.support.uitls.DataPageUtil;

/**
 * DAO class
 * @author robot
 */
public class VideoFileDAO extends BasePageSearchDAO<VideoFile> {

	/**
	 * Query SQLs
	 */
	private static final String SQL_GET_TOTAL_COUNT = "select count(*) from hv_video_file";

	private static final String SQL_GET_ALL_VIDEOFILES = "select * from hv_video_file";

	private static final String SQL_GET_VIDEOFILE_BY_PK = "select * from hv_video_file where uid = ?";

	/**
	 * CUR SQLs
	 */
	private static final String SQL_INSERT_VIDEOFILE = "insert into hv_video_file(uid,orig_file_name,snapshot,file_size,mime_type,play_len,file_ext,create_by_uid,create_date,last_upd_by_uid,last_upd_date) values (?,?,?,?,?,?,?,?,?,?,?)";

	private static final String SQL_UPDATE_VIDEOFILE = "update hv_video_file set orig_file_name=?, snapshot=?, file_size=?, mime_type=?, play_len=?, file_ext=?, last_upd_by_uid=?, last_upd_date=? where uid=?";

	private static final String SQL_DEL_VIDEOFILE = "delete from hv_video_file where uid=?";
	
	private static final String SQL_UPDATE_VIDEOFILE_SNAPSHOT = "update hv_video_file set snapshot=?, play_len=? where uid=?";

	private static final String SQL_UPDATE_VIDEOFILE_PLAYLEN = "update hv_video_file set play_len=? where uid=?";

	private static final String SQL_UPDATE_VIDEO_FILE = "update hv_video_file set orig_file_name=?, file_size=?, mime_type=?, play_len=?, file_ext=?, last_upd_by_uid=?, last_upd_date=? where uid=?";


	/**
	 * Get Count.
	 */
	public int getTotalCount() {
		return dbe.getCount(SQL_GET_TOTAL_COUNT);
	}

	/**
	 * Get all list.
	 */
	public List<VideoFile> getAllVideoFiles() {
		return dbe.list(SQL_GET_ALL_VIDEOFILES, VideoFile.class);
	}

	/**
	 * Get all list by pagination.
	 */
	public List<VideoFile> getVideoFiles(int start, int end) {
		SQLWithParams swp = DialectSyntaxParser.getParser().getPaginationSQLWithParams(SQL_GET_ALL_VIDEOFILES, null, start, end);
		return dbe.list(swp.getSql(), VideoFile.class, swp.getParams());
	}
	
	public void loadVideoFilesByPage(DataPageUtil<VideoFile> dataPageUtil) {
		String countSql = "select count(*) from hv_video_file where 1=1";
		String sql = " select * from hv_video_file where 1=1";
		SQLWithParams swp = initSQLWithParams(dataPageUtil, null, countSql, sql);
		dataPageUtil.setResult(dbe.list(swp.getSql(), VideoFile.class, swp.getParams()));
	}

	/**
	 * Get VideoFile by primary key.
	 */
	public VideoFile getVideoFile(String uid) {
		VideoFile v = dbe.load(SQL_GET_VIDEOFILE_BY_PK, VideoFile.class, uid);
		return v;
	}

	/**
	 * Insert one row.
	 */
	public void insertVideoFile(DOVideoFile videoFile) {
		dbe.execute(SQL_INSERT_VIDEOFILE, videoFile.getUid(), videoFile.getOrigFileName(), videoFile.getSnapshot(),
				videoFile.getFileSize(), videoFile.getMimeType(), videoFile.getPlayLen(), videoFile.getFileExt(),
				videoFile.getCreateByUid(), videoFile.getCreateDate(), videoFile.getLastUpdByUid(), videoFile.getLastUpdDate());
	}

	/**
	 * Update by PK.
	 */
	public void updateVideoFile(DOVideoFile videoFile) {
		
		if (StringUtils.isNotBlank(videoFile.getSnapshot()) && StringUtils.isBlank(videoFile.getOrigFileName())) {
			/*
			 * only update snapshot
			 */
			dbe.execute(SQL_UPDATE_VIDEOFILE_SNAPSHOT, videoFile.getSnapshot(), videoFile.getPlayLen(), videoFile.getUid());
		} else if (StringUtils.isNotBlank(videoFile.getOrigFileName()) && StringUtils.isBlank(videoFile.getSnapshot())) {
			/*
			 * only update video info 
			 */
			dbe.execute(SQL_UPDATE_VIDEO_FILE, videoFile.getOrigFileName(), videoFile.getFileSize(), videoFile.getMimeType(),
					videoFile.getPlayLen(), videoFile.getFileExt(), videoFile.getLastUpdByUid(), videoFile.getLastUpdDate(),
					videoFile.getUid());
		} else if (StringUtils.isNotBlank(videoFile.getSnapshot()) && StringUtils.isNotBlank(videoFile.getOrigFileName())) {
			/*
			 * update all info
			 */
			dbe.execute(SQL_UPDATE_VIDEOFILE, videoFile.getOrigFileName(), videoFile.getSnapshot(), videoFile.getFileSize(),
					videoFile.getMimeType(), videoFile.getPlayLen(), videoFile.getFileExt(), videoFile.getLastUpdByUid(),
					videoFile.getLastUpdDate(), videoFile.getUid());
		} else {
			dbe.execute(SQL_UPDATE_VIDEOFILE_PLAYLEN, videoFile.getPlayLen(), videoFile.getUid());
		}
	}

	/**
	 * Delete row by PK.
	 */
	public void deleteVideoFile(String uid) {
		dbe.execute(SQL_DEL_VIDEOFILE, uid);
	}

}